package linuxbox.poll.taglib;

import javax.servlet.jsp.*;
import javax.servlet.jsp.tagext.*;
import java.util.*;

import linuxbox.poll.logic.*;
import linuxbox.admin.logic.UserManager;


public class VoteResults extends BodyTagSupport
{
  private int pollID;
  private boolean showVoter;
  private String percentImage;
  private UserManager um = UserManager.getInstance();

  public void setPollID(int id)
  {
    pollID = id;
  }
	public void setShowVoter(String aValue)
	{
		showVoter = new Boolean(aValue).booleanValue();
	}
	public void setPercentImage(String aValue)
	{
		percentImage = aValue;
	}

	public int doStartTag() throws JspException
	{
		try
		{
			JspWriter out = pageContext.getOut();

			BoxPoll home = new BoxPoll();
//			BoxPollEntry entry = home.get(new BaseIntKey(pollID));
//			if ( entry == null )
//    		return SKIP_BODY;

      out.print("<TR align=left bgColor=#eeeeee>");
      out.print("<TH noWrap width=\"50%\"><FONT face=arial,helvetica color=#000000 size=-1>Choices</FONT></TH>");
      out.print("<TH noWrap align=middle width=\"1%\"><FONT face=arial,helvetica color=#000000 size=-1>Votes</FONT></TH>");
      out.print("<TH align=middle width=\"1%\"><FONT face=arial,helvetica color=#000000 size=-1>%</FONT></TH>");

      //TODO: check entry.getShowResult() and userID to show results
      int count = home.getTotalReplies(pollID);
      out.print("<TH noWrap align=left><FONT face=arial,helvetica color=#000000 size=-1><B>");
      if (count==0)
        out.print("No replies");
      else if (count==1)
        out.print("1 reply");
      else
        out.print(count + " replies");
      out.print("</B></FONT></TH>");
      if (showVoter)
        out.print("<TH noWrap align=left><FONT face=arial,helvetica color=#000000 size=-1><B>Respondents </B></FONT></TH>");
			out.print("</TR>");

			Iterator it = home.getChoices(pollID);
			if ( it != null && it.hasNext())
			{
			  while ( it.hasNext())
			  {
			    BoxPollChoiceEntry choice = (BoxPollChoiceEntry) it.next();
          out.print("<TR vAlign=top align=left>");
          out.print("<TD width=\"50%\"><FONT size=-1>");
          out.print(choice.getChoice());
          out.print("</FONT></TD>");
          int votes = home.getCountForChoice(choice.getID());
          out.print("<TD align=middle width=\"1%\"><FONT size=-1>");
          out.print(votes);
          out.print("</FONT></TD>");
          out.print("<TD align=middle width=\"1%\"><FONT size=-1>");
          int percent = 0;
          if ( count > 0 )
            percent = votes*100/count;
          out.print(percent);
          out.print("% </FONT></TD>");

          out.print("<TD>");
          if ( percent > 0 )
          {
            out.print("<IMG height=14 src=\"");
            out.print(percentImage);
            out.print("\" width=\"");
            out.print(percent);
            out.print("%\"> ");
          }
          out.print("</TD>");
          if (showVoter)
          {
            out.print("<TD><FONT size=-1>");
            //get voters
            Iterator it1 = home.getRepliesForChoice(choice.getID());
            boolean first=true;
            while (it1.hasNext())
            {
              BoxPollReplyEntry reply = (BoxPollReplyEntry) it1.next();
              if (!first)
              {
                out.print(",");
              }
              else
                first = false;

              out.print(um.getUserDisplayName(reply.getUserID(), um.USERNAME_FORMAT_LOGINID));
            }
            out.print("<BR></FONT></TD>");
          }
          out.print("</TR>");
			  }
			}
			else
			{
			  out.print("There is no choices setup for this poll!");
			}

			out.flush();
		}
		catch(Exception e)
		{
			e.printStackTrace();
			throw new JspException(e.getMessage());
		}

		return SKIP_BODY;
	}
}
